package com.wzq.upms.common.utils;

import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 处理属性文件工具类
 * 
 * project：pub-core
 * 
 * $Author:Jeffy Chen
 * @version $Revision:$ 
 * @Modify $Date:$
 * @create time：2014-4-26 下午11:02:47
 *
 */
public class PropertyUtils {
	private static Logger logger = LogManager.getLogger(PropertyUtils.class);
	/**
	 * PropsUtil
	 */
	private static PropertyUtils instance;

	/**
	 * Properties
	 */
	private Properties props = new Properties();

	/**
	 * PropsUtil Object
	 */
	private PropertyUtils() {
		ClassLoader classLoader = getClass().getClassLoader();
		try {
			URL url = classLoader.getResource("lianxian-blog.properties");
			if (url != null) {
				InputStream is = url.openStream();
				props.load(is);
				is.close();
			}
		} catch (Exception e) {
			logger.debug("Load properties file failed!", e);
		}
	}
	
	/**
	 * Get Instance of PropsUtil
	 * 
	 * @return The instance of PropsUtil
	 */
	private static PropertyUtils getInstance() {
		synchronized (PropertyUtils.class) {
			if (instance == null) {
				instance = new PropertyUtils();
			}
		}
		return instance;
	}
	
	/**
	 * Get Properties
	 * 
	 * @return Properties
	 */
	public static Properties getProperties() {
		return getInstance().props;
	}

	/**
	 * If contains key
	 * 
	 * @param key
	 *            Key
	 * @return Boolean, false or true
	 */
	public static boolean containsKey(String key) {
		return getInstance().props.containsKey(key);
	}

	/**
	 * Get Value of key
	 * 
	 * @param key
	 *            Key
	 * @return The value of Key
	 */
	public static String get(String key) {
		return getInstance().props.getProperty(key);
	}

	/**
	 * Set value for key
	 * 
	 * @param key
	 *            Key
	 * @param value
	 *            The value of Key
	 */
	public static void set(String key, String value) {
		getInstance().props.setProperty(key, value);
	}

	/**
	 * Get a array of value of key
	 * 
	 * @param key
	 *            Key
	 * @return A array of value
	 */
	public static String[] getArray(String key) {
		String value = get(key);
		if (value == null) {
			return new String[0];
		} else {
			return StringUtils.split(key,',');
		}
	}
}
